import { _decorator, Button, Component, Label, Node, ProgressBar, Sprite, tween, UIOpacity, Vec3 } from 'cc';
import { Constants, FortressConfigs } from '../../game/Constants';
import { GameDefine } from '../../game/GameDefine';
import { EventListener } from '../../core/EventListener';
import { FortressManager } from '../../fortress/FortressManager';
import { AudioManager } from '../../game/AudioManager';
import { UILottery } from '../UILottery';
import { EnemyManger } from '../../actor/EnemyManger';
const { ccclass, property } = _decorator;

@ccclass('GameItem')
export class GameItem extends Component {
    // 要塞参数
    @property(ProgressBar) hpBar: ProgressBar = null!;
    @property(Label) lbl_atk: Label = null!;
    @property(Label) lbl_def: Label = null!;
    @property(Label) lbl_gold: Label = null!;
    @property(Button) btn_fire: Button = null!;
    @property(Node) fireHandTips: Node = null!;

    @property(Node) goldCollectNode : Node = null!;

    // 抽奖功能
    @property(Button) btn_skill: Button = null!;
    @property(Sprite) skillImage: Sprite = null!;
    @property(Node) skillHandTips: Node = null!;
    @property(Node) skillMask: Node = null!;

    // BOSS来袭UI闪烁
    @property(Node) BossWarning: Node = null!;
    @property(Node) redBoss: Node = null!;
    @property(Node) midBoss: Node = null!;
    @property(Node) upText: Node = null!;
    @property(Node) downText: Node = null!;

    // 进度条
    @property({ type: ProgressBar, displayName: "进度条" })
    gameBar: ProgressBar = null!;

    start() {
        // 初始化要塞
        EventListener.on(Constants.update_fortress_level, this.initUI, this);
        EventListener.on('update_gold', this.initUI, this);
        this.btn_fire.node.on(Button.EventType.CLICK, this.onPlayFire, this);
        this.btn_skill.node.on(Button.EventType.CLICK, this.onPlayerSkill, this);
        this.skillImage = this.btn_skill.node.getComponent(Sprite)!;

        this.showSkillButton(false);
        this.playBossWarnAnim();
        this.initUI();
    }

    private initUI() {
        tween(this.hpBar).to(0.5, { progress: 1 }).start();
        this.lbl_atk.string = `${FortressConfigs[GameDefine.FortressLevel - 1].atk}`;
        this.lbl_def.string = `${FortressConfigs[GameDefine.FortressLevel - 1].def}`;
        this.lbl_gold.string = `${GameDefine.goldNum}`;
    }


    private onPlayFire() {
        FortressManager.Instance.onPlayFire();
    }

    /** 点击抽奖按钮 */
    private onPlayerSkill() {
        this.onSkillClick?.();
        AudioManager.Instance.playUIClick();
        this.showSkillButton(false);
        UILottery.Instance.onShow();
    }

    private onSkillClick: Function = null;
    /** 显示抽奖提醒 */
    public showSkillButton(show: boolean, cb: Function = null) {
        if (show) {
            this.btn_skill.interactable = true;
            this.skillImage.grayscale = false;
            this.skillHandTips.active = true;
            this.skillMask.active = true;
            this.onSkillClick = cb;
        } else {
            this.btn_skill.interactable = false;
            this.skillImage.grayscale = true;
            this.skillHandTips.active = false;
            this.skillMask.active = false;
        }
    }

    /** Boss来袭红色警告UI闪烁 */
    playBossWarning() {
        const opacity = this.BossWarning.getComponent(UIOpacity);
        opacity.opacity = 255;
        this.scheduleOnce(() => {
            opacity.opacity = 0;
        }, 2);
    }


    private playBossWarnAnim() {
        const opacity = this.redBoss.getComponent(UIOpacity);
        tween(opacity)
            .to(0.5, { opacity: 255 })
            .to(0.5, { opacity: 0 })
            .union()
            .repeatForever()
            .start();

        tween(this.midBoss)
            .to(0.5, { scale: new Vec3(1, 1, 1) })
            .to(0.5, { scale: new Vec3(0.9, 0.9, 1) })
            .union()
            .repeatForever()
            .start();

    }
}


